﻿#include "ProcessBlock.h"
namespace CVV
{
namespace FlowChart
{
ProcessBlock::ProcessBlock()
{

}

ProcessBlock::ProcessBlock(QString &name,QRect &position,double zoom,QString &state,QMargins margins)
{
    setZoom(zoom);
    setPostion(position);
    setMargin(margins);
    setName(name);
    setState(state);
}

ProcessBlock::~ProcessBlock()
{

}

void ProcessBlock::paint(QPainter *canvas)
{
    double y=postion().y();
    double x=postion().x();
    double width=postion().width();
    double height=postion().height();
    double hcenter = x + width / 2;
    double a = 50 * zoom();
    double b = static_cast<double>(m_bScale)* a;
    double bottom = y + height;
    double lineScale=static_cast<double>(m_lineScale);
    SetNewPen(2)
    canvas->drawLine(QLineF(hcenter, y-0.5, hcenter, y + lineScale * zoom()));
    ProcessBlock::drawBottomArrow(canvas, QPointF(hcenter, y + lineScale * zoom()),
                                  QSize(static_cast<int>(6 * zoom()), static_cast<int>(12 * zoom())));
    QRectF rect(hcenter - b/2, y + lineScale * zoom(), b, a);
    QRectF textRect(hcenter - b/2 + 4 * zoom(), y + lineScale * zoom(), b - 8 * zoom(), a - 8 * zoom());

    if(runState().compare("RunSuccessfully") == 0)
    {
        QPen pe(m_runSuccessColor);
        pe.setWidthF(2);
        canvas->setPen(pe);
    }
    else if(runState().compare("RunError") == 0)
    {
        QPen pe(m_runErrorColor);
        pe.setWidthF(2);
        canvas->setPen(pe);
    }

    if(0 <= getReferenceStatus())
    {
        QColor color = m_unReferencedColor[0];
        if(m_unReferencedColor.size() >= getReferenceStatus())
        {
            color = m_unReferencedColor[getReferenceStatus()];
        }
        QPen pe(color);
        pe.setWidthF(2);
        canvas->setPen(pe);
    }

    QBrush brush = canvas->brush();
    canvas->setBrush(m_backgroundColor);
    canvas->drawRect(rect);
    canvas->setBrush(brush);

    UseOldPen
    canvas->drawText(textRect, Qt::AlignCenter | Qt::TextWrapAnywhere, name());

    SetNewPen(2)
    canvas->drawLine(QLineF(hcenter, y + lineScale * zoom()+a, hcenter, bottom+0.5));
    UseOldPen
}

QString ProcessBlock::getBlockDom()
{
    QString xmlStr = QString("<algorithm><branch><process id=\"%1\" category=\"%2\" code=\"%3\" title=\"%4\" state=\"normal\" isPlugin=\"true\">%5</process></branch></algorithm>");
    return  xmlStr;
}

}
}
